package com.zzr.demo.redis.controller;

import com.zzr.demo.redis.service.CompactCounterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: xiaomi
 * @date: 2021/10/11
 * @description: 紧凑计数器
 * 出于节省空间的考虑，使用一个大位图，记录不同整型主键下的计数
 */
@RestController
@RequestMapping("counter/compact/logging")
@Api(tags = "紧凑计数器：记录用户登录次数")
@Slf4j
public class LoggingCompactCounterController {


    @Autowired
    CompactCounterService compactCounterService;

    @GetMapping("/init/{userId}")
    @ApiOperation("初始化主键计数，默认0")
    public boolean init(@PathVariable("userId") long userId) {
        compactCounterService.init(userId);
        return true;
    }

    @GetMapping("/increase/{userId}")
    @ApiOperation("计数器增加")
    public long increase(@PathVariable("userId") long userId) {
        long increase = compactCounterService.increase(userId);
        return increase;
    }
    

}
